<template>
  <div class="father">
    <van-cell>
      <!-- 头像 -->
      <div class="left">
        <van-image round fit="cover" :src="cmt.aut_photo" />
      </div>
      <!-- 中间信息 -->
      <div class="mid">
        <div class="name">{{ cmt.aut_name }}</div>
        <div class="details">{{ cmt.content }}</div>
        <!-- 评论时间和回复条数 -->
        <div class="comments">
          <div class="time">{{ cmt.pubdate | relvTime }}</div>
        </div>
      </div>
    </van-cell>
    <!-- 分割线 -->
    <van-divider>回复列表</van-divider>
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="(item, idx) in list" :key="idx">
        <div class="cmt-box">
          <!-- 头像 -->
          <van-image round fit="cover" :src="item.aut_photo" />
          <!-- 中间信息 -->
          <div class="mid">
            <div class="name">{{ item.aut_name }}</div>
            <div class="details">{{ item.content }}</div>
            <!-- 评论时间和回复条数 -->
            <div class="comments">
              <div class="time">{{ item.pubdate | relvTime }}</div>
            </div>
          </div>
        </div>
      </van-cell>
    </van-list>
    <!-- 发表区域 -->
    <div class="send-box">
      <van-field
        v-model="sms"
        @keyup.enter="send"
        center
        clearable
        placeholder="发表你的观点"
      >
        <template #button>
          <van-button size="small" @click="send" type="info">发布</van-button>
        </template>
      </van-field>
    </div>
  </div>
</template>

<script>
import { getCommentsAPI, sendCommentsAPI } from '@/api'
export default {
  name: 'replyInfo',
  props: ['cmt'],
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      lastID: null,
      endID: '',
      sms: ''
    }
  },
  methods: {
    async onLoad () {
      const res = await getCommentsAPI({
        type: 'c',
        source: this.cmt.com_id,
        offset: this.lastID,
        limit: 4
      })
      console.log('cmt', this.cmt)
      //   console.log('this.$route.params.id', this.$route.params.id)
      //   console.log('res', res)
      this.list.push(...res.data.data.results)
      this.lastID = res.data.data.last_id
      if (res.data.data.end_id === this.lastID) {
        this.finished = true
      }
      this.loading = false
    },
    // 发表评论
    async send () {
      if (!this.sms) return this.$toast('请输入评论内容')
      const res = await sendCommentsAPI({
        target: this.cmt.com_id,
        content: this.sms,
        art_id: this.$route.params.id
      })
      this.list.unshift(res.data.data.new_obj)
      this.sms = ''
      this.$toast.success('评论成功')
      this.cmt.reply_count++
      console.log('发表评论', res)
    }
  }
}
</script>

<style lang="less" scoped>
.father {
  padding-bottom: 100px;
  .van-cell {
    .mid {
      flex: 1;
      margin-left: 10px;
      .comments {
        display: flex;
        .van-button {
          width: 50px;
        }
      }
    }
    ::v-deep .van-cell__value {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .van-image {
        width: 50px;
        height: 50px;
      }
    }
  }
}
.send-box {
  width: 100%;
  position: fixed;
  left: 0;
  bottom: 0;
  ::v-deep .van-field__body {
    width: 100%;
  }
}
.cmt-box {
  display: flex;
  justify-content: center;
  align-items: center;
  .van-image {
    width: 50px;
    height: 50px;
  }
  .mid {
    margin-left: 10px;
    flex: 1;
  }
}
</style>
